package com.example.service;


import com.example.mapper.FilesMapper;
import com.example.model.Files;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class FilesService {
    @Autowired
    FilesMapper filesMapper;
    private static final String FILE_PATH_PREFIX = "D:/ketangpai666/uploads/";

    public String insertFile(Files files, MultipartFile file,String studentId, int fileId) throws IOException {
        // 创建一个表示日期时间的Date对象
        Date date = new Date();

// 创建一个SimpleDateFormat对象来定义目标格式
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");

// 使用SimpleDateFormat格式化Date对象为目标格式的字符串
        String formattedDateTime = targetFormat.format(date);
        System.out.println(formattedDateTime);

        if (filesMapper.findTaskEndTime(studentId, formattedDateTime) != null ){
            String fileName = file.getOriginalFilename();

            Path filePath = Paths.get(FILE_PATH_PREFIX, fileName);
            file.transferTo(filePath.toFile());
//            files.setStudentName(studentId);
            files.setStudentId(studentId);

            files.setFileName(fileName);
            files.setFileId(fileId);

//            files.setCreatFileData(date);
            files.setCreateFileData(formattedDateTime);

            filesMapper.insertFiles(files);
        }else {
            System.out.println("时间超时");
            return "already exist";
        }
        return "already upload";

    }
    public String findFileId(String fileId){
        return filesMapper.findFileId(fileId);
    }
public List<Files> findFileAll(String taskId){
    return filesMapper.findFileAll(taskId);
}
    public void status(int fileId,String statusFile) {
        filesMapper.setStatus(statusFile,fileId);
    }
    public String [] status(String taskId){
        return filesMapper.queryStatus(taskId);
    }
}
